package com.example.onion.hxchat.room.repositorywc;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import com.example.onion.hxchat.room.dao.InfoDao;
import com.example.onion.hxchat.room.dao.TeamNotificationDao;
import com.example.onion.hxchat.room.entity.Info;
import com.example.onion.hxchat.room.entity.TeamNotification;
import com.example.onion.hxchat.room.wcdatabase.MyDataBaseWc;

import java.util.List;

/**
 * @author:onion
 * @data: 2020/8/21
 */
public class TeamNotificationRepository {
    /**
     * Data list from Dao.
     */
    private LiveData<List<TeamNotification>> teamNotifications;


    private TeamNotificationDao teamNotificationDao;

    private MyDataBaseWc database;

    public TeamNotificationRepository(Context context) {
        //获取数据库实例，数据库采用单例模式
        database = MyDataBaseWc.getInstance(context.getApplicationContext());
        teamNotificationDao = database.getTeamNotificationDao();
        teamNotifications = teamNotificationDao.getAll();
    }

    /**
     * 只在model用到了  获取当前用户的对话框信息使用的下面的getUsersInfo方法
     * @return The list of users
     */
    public LiveData<List<TeamNotification>> getAllInfos() {
        return teamNotifications;
    }

    /**
     * 获得当前登录用户收到的相关队伍通知的小心
     * @param userID 当前登录用户id
     * @return
     */
    public LiveData<List<TeamNotification>> getTeamNotificationCur(String userID)
    {
        return teamNotificationDao.getTeamNotificationCur(userID);
    }

    /**
     * 保存接收到的队伍相关的通知消息
     * @param teamNotifications
     */
    public void insertTeamNotification(TeamNotification... teamNotifications)
    {
        new TeamNotificationRepository.InsertTeamNotificationAsyncTask(teamNotificationDao).execute(teamNotifications);
    }

    /**
     * 删除接收到的队伍相关的通知消息
     * @param teamNotifications
     */
    public void deleteTeamNotification(TeamNotification... teamNotifications)
    {
        new TeamNotificationRepository.DeleteTeamNotificationAsyncTask(teamNotificationDao).execute(teamNotifications);
    }


    /**部类，用于在子线程中访问数据库，如果有多个操作的话，每一个操作都要对应一个AsyncTask
     * 插入相关队伍通知的任务类
     */
    static class InsertTeamNotificationAsyncTask extends AsyncTask<TeamNotification,Void,Void> {

        private TeamNotificationDao teamNotificationDao;

        public InsertTeamNotificationAsyncTask(TeamNotificationDao infoDao) {
            this.teamNotificationDao = infoDao;
        }

        @Override
        protected Void doInBackground(TeamNotification... teamNotifications) {
            teamNotificationDao.insertTeamNotification(teamNotifications);
            return null;
        }
    }


 /**部类，用于在子线程中访问数据库，如果有多个操作的话，每一个操作都要对应一个AsyncTask
     * 插入相关队伍通知的任务类
     */
    static class DeleteTeamNotificationAsyncTask extends AsyncTask<TeamNotification,Void,Void> {

        private TeamNotificationDao teamNotificationDao;

        public DeleteTeamNotificationAsyncTask(TeamNotificationDao infoDao) {
            this.teamNotificationDao = infoDao;
        }

        @Override
        protected Void doInBackground(TeamNotification... teamNotifications) {
            teamNotificationDao.deleteTeamNotification(teamNotifications);
            return null;
        }
    }




}
